package com.cms.controller;


import com.cms.dto.SearchDTO;
import com.cms.entity.Student;
import com.cms.service.StudentService;
import com.cms.vo.Result;
import com.google.gson.Gson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;


    @PostMapping(value = "/save", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save(Student student){
        Integer save = studentService.save(student);
        if (save != 1) {
            return new Gson().toJson(Result.error(501, "学生信息保存失败"));
        }
        return new Gson().toJson(Result.success("学生信息保存成功"));
    }


    @GetMapping(value = "/list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list() {
        return new Gson().toJson(Result.success(studentService.list()));
    }


    @PostMapping("/delete")
    @ResponseBody
    public String delete(Integer sid) {
        Integer delete = studentService.delete(sid);
        if (delete != 1) {
            return new Gson().toJson(Result.error(501, "学生信息删除失败"));
        }
        return new Gson().toJson(Result.success("学生信息删除成功"));
    }


    @GetMapping("/view")
    public String view(){
        return "StudentScoreSimple";
    }


    @GetMapping(value = "/search", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String search(SearchDTO searchDTO) {
        return new Gson().toJson(Result.success(studentService.search(searchDTO)));
    }


    @PostMapping("/update")
    @ResponseBody
    public String update(Student student) {
        Integer update = studentService.update(student);
        if (update != 1) {
            return new Gson().toJson(Result.error(501, "学生信息更新失败"));
        }
        return new Gson().toJson(Result.success("学生信息更新成功"));
    }




    @GetMapping("/studentPage")
    public String studentPage(){
        return "saveStudent";
    }


    @GetMapping("/listPage")
    public String listPage(){
        return "listStudent";
    }


}
